SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROC [dbo].[SP_TASALD_CORP_Q07]  
/*---------------------------------------------------------------------*/  
/*----- Empresa          : OFISIS S.A.                            -----*/  
/*----- Cliente          : OFISIS S.A.                            ----*/  
/*----- Sistema          : Contabilidad                           -----*/  
/*----- Modulo           : Empresa                                -----*/  
/*----- Programa         : Balance de Comprobacion Mensual Corporativo(CON QUIEBRE)-----*/  
/*----- Nombre           : SP_TASALD_CORP_Q07                   -----*/  
/*----- Script           : TASALD_EMPR                            -----*/  
/*----- Desarrollado     : Franco Gamboni Koifman                 -----*/  
/*----- Fecha            : 23/06/1997                             -----*/  
/*----- Base de Datos    : MS SQL Server                          -----*/  
/*----- Version BD       : 7.0                                    -----*/  
/*----- Invoca a SP      :                                        -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- MODIFICADO POR   : PERCY GARCIA                           -----*/  
/*----- FECHA MODIF      : 06/12/2004                             -----*/  
/*----- DETALLE MODIF    : SE AUMENTO FILTROS PARA VISUALIZAR FILAS CON MONTOS -----*/  
/*---------------------------------------------------------------------*/  
/*---------------------------------------------------------------------*/  
/*----- Drop Proc SP_TASALD_CORP_Q07                            -----*/  
/*----- Grant Execute On SP_TASALD_CORP_Q07 To PUBLIC           -----*/  
/*----- SP_TASALD_CORP_Q07 2004, 12, 5, 'U', 'REP', 'T'      -----*/  
/*---------------------------------------------------------------------*/  
  
@INNU_ANNO  TD_IN_004_ANNO,  
@INNU_MESE  TD_IN_002_MESE,  
@INNU_DGTO  TD_IN_001,  
@ISTI_PRES  TD_VC_001,  
@ISTI_REPO  TD_VC_003,  
@ISFL_MONT  TD_VC_001  
as  
Create Table #TWSALD_CORP_Q07_1  
(CO_CNTA_CORP Char(10) NULL,   
 DE_CNTA_CORP Char(50) NULL,  
 SALD_ANT    Numeric(16,4)   NULL,  
 DEBE_MES     Numeric(16,4)    NULL,  
 HABER_MES    Numeric(16,4)    NULL,   
 SALD_DEBE    Numeric(16,4)    NULL,  
 SALD_HABER   Numeric(16,4)    NULL,   
 ACTIVO       Numeric(16,4)    NULL,  
 PASIVO       Numeric(16,4)    NULL,  
 PERDIDAS     Numeric(16,4)    NULL,  
 GANANCIAS    Numeric(16,4)    NULL,  
 PERDIDAFUN  Numeric(16,4)    NULL,  
 GANANCIAFUN  Numeric(16,4)    NULL)  
  
Declare                                                                                                     
@CSCO_CNTA  TD_VC_010, @CSNO_CNTA  TD_VC_050,  
@VSCO_CNTA       TD_VC_002,  
@CSCO_BALA  TD_VC_003,  
@VSCO_BALA  TD_VC_002,  
@CNIM_SALD_ANTE  TD_NU_016_004,  
@VNIM_DEBE_MESE  TD_NU_016_004,  
@VNIM_HABE_MESE  TD_NU_016_004,  
@VNIM_SALD_DEBE  TD_NU_016_004,  
@VNIM_SALD_HABE  TD_NU_016_004,  
@VNTO_CALC       TD_NU_016_004,  
@VNIM_MES2       TD_NU_016_004                                                                                                                                                                                                                                 
  
                                  
If @INNU_DGTO != 0   
 Begin  
  Declare CU_TASALD_CORP_Q07_1 Cursor  
  For  
   Select  T2.CO_CNTA_CORP, MAX(RTRIM(T2.DE_CNTA_CORP)),  
    Isnull(SUM(ROUND(ISNULL(T1.IM_CARG_MESE,0),2)), 0) - Isnull(SUM(ROUND(ISNULL(T1.IM_ABON_MESE,0),2)), 0), T2.CO_BALA  
   From TMPLAN_CORP T2  
     Left Outer Join TASALD_CORP T1
     On T1.NU_ANNO = @INNU_ANNO  
     And T1.NU_MESE < @INNU_MESE 
	 And T1.CO_CNTA_CORP = T2.CO_CNTA_CORP 
	 And T1.TI_AUXI_CORP IS NULL 
	 And T1.CO_AUXI_CORP IS NULL   
   Where DATALENGTH(RTRIM(T2.CO_CNTA_CORP)) = @INNU_DGTO 
   Group By T2.CO_CNTA_CORP, T2.CO_BALA  
 End  
  
If @INNU_DGTO = 0   
 Begin  
  Declare CU_TASALD_CORP_Q07_1 Cursor  
  For  
   Select  T2.CO_CNTA_CORP, MAX(RTRIM(T2.DE_CNTA_CORP)),  
    Isnull(SUM(ROUND(isnull(T1.IM_CARG_MESE,0),2)), 0) - Isnull(SUM(ROUND(isnull(T1.IM_ABON_MESE,0),2)), 0), T2.CO_BALA  
   From TMPLAN_CORP T2  
	 Left Outer Join TASALD_CORP T1
	 On T1.NU_ANNO = @INNU_ANNO  
     And T1.NU_MESE < @INNU_MESE 
	 And T1.CO_CNTA_CORP = T2.CO_CNTA_CORP 
	 And T1.TI_AUXI_CORP IS NULL 
	 And T1.CO_AUXI_CORP IS NULL 
   Group By T2.CO_CNTA_CORP, T2.CO_BALA  
   Order By T2.CO_CNTA_CORP, T2.CO_BALA  
 End  
  
                                                                                                                                                                   
Open CU_TASALD_CORP_Q07_1  
Fetch CU_TASALD_CORP_Q07_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_SALD_ANTE, @CSCO_BALA  
                                                                                                                                                                          
While @@Fetch_status = 0  
Begin  
  
Select @VNIM_DEBE_MESE = 0  
Select @VNIM_HABE_MESE = 0  
  
Select @VNIM_DEBE_MESE  = ISNULL(SUM(ROUND(Isnull(T1.IM_CARG_MESE, 0),2)),0),  
       @VNIM_HABE_MESE = ISNULL(SUM(ROUND(Isnull(T1.IM_ABON_MESE, 0),2)),0)  
From   TASALD_CORP T1, TMPLAN_CORP T2   
Where  T1.NU_ANNO  = @INNU_ANNO  and  
       T1.NU_MESE  = @INNU_MESE        and   
       T1.CO_CNTA_CORP = RTRIM(@CSCO_CNTA) and  
       T1.TI_AUXI_CORP IS NULL           and  
       T1.CO_AUXI_CORP IS NULL           and  
       T1.CO_CNTA_CORP = T2.CO_CNTA_CORP  
Group By T1.CO_CNTA_CORP  
If @VNIM_DEBE_MESE IS NULL  
 Select @VNIM_DEBE_MESE = 0  
If @VNIM_HABE_MESE IS NULL  
 Select @VNIM_HABE_MESE = 0  
                                  
                                   
Select @VNTO_CALC = ROUND(@VNIM_DEBE_MESE,2) - ROUND(@VNIM_HABE_MESE,2)  
/*-----------------------------GANANCIAS------------------------------------*/  
                                                                                                                                                                                 
If @VNTO_CALC >= 0  
 Begin  
  Select @VSCO_CNTA = SUBSTRING(@CSCO_CNTA,1,2)  
  Select @VSCO_BALA = SUBSTRING(@CSCO_BALA,1,2)  
  /*------- CuentAs de Inventario ----------*/  
   If @VSCO_CNTA <'60'  
     Begin   
       Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP, DE_CNTA_CORP, SALD_ANT,  
         DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
         PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
       Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,   
         @VNIM_HABE_MESE,@VNTO_CALC, 0, @VNTO_CALC, 0, 0, 0, 0, 0 )  
     End  
    Else  
   /*------- GANANCIAS y PERDIDAS por Naturaleza ---------*/  
   If @VSCO_BALA = '10'  
    Begin   
     Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP,SALD_ANT,           
       DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
       PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
     Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,  
       @VNIM_HABE_MESE, @VNTO_CALC, 0, 0, 0, @VNTO_CALC, 0, 0, 0 )  
     End  
   Else   
   /*--------------GANANCIAS y PERDIDAS por Naturaleza/Funci=n-----------------*/  
   If @VSCO_BALA = '11'  
    Begin  
     Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP,SALD_ANT,           
        DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
        PASIVO, PERDIDAS,GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
     Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,  
        @VNIM_HABE_MESE, @VNTO_CALC, 0, 0, 0, @VNTO_CALC, 0, @VNTO_CALC, 0)  
    End   
  Else  
   /*------- GANANCIAS y PERDIDAS por Funci=n ---------*/  
 If @VSCO_BALA = '01'  
    Begin   
      Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP,SALD_ANT,           
       DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
       PASIVO, PERDIDAS,GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
      Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,  
       @VNIM_HABE_MESE, @VNTO_CALC, 0, 0, 0, 0, 0, @VNTO_CALC, 0)  
    End   
  Else  
   Begin                                                                                                                                                                                                                                                       
    Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP, DE_CNTA_CORP, SALD_ANT,  
       DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
       PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
      Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,   
       @VNIM_HABE_MESE, @VNTO_CALC, 0, 0, 0, 0, 0, 0, 0 )  
   End   
  End  
  /*---------------------------------PERDIDAS-------------------------------------*/  
 If @VNTO_CALC < 0  
  Begin  
    Select @VSCO_CNTA = SUBSTRING(@CSCO_CNTA,1,2)  
    Select @VSCO_BALA = SUBSTRING(@CSCO_BALA,1,2)  
    /*------- CuentAs de Inventario ---------*/  
     If @VSCO_CNTA < '60'  
      Begin  
         Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT,  
          DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER,ACTIVO,   
          PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
         Values (RTRIM(@CSCO_CNTA),RTRIM(@CSNO_CNTA),@CNIM_SALD_ANTE,@VNIM_DEBE_MESE,  
          ( @VNIM_HABE_MESE ), 0, Abs(@VNTO_CALC), 0, Abs(@VNTO_CALC), 0, 0,0, 0 )  
      End  
     Else  
     /*------- GANANCIAS y PERDIDAS por Naturaleza ---------*/  
      If @VSCO_BALA = '10'  
       Begin   
          Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT,           
           DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
           PASIVO, PERDIDAS,GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
          Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,  
           @VNIM_HABE_MESE, 0, Abs(@VNTO_CALC), 0, 0,  0 , Abs(@VNTO_CALC), 0, 0)  
       End  
     Else  
   /*------------GANANCIAS y PERDIDAS por Naturaleza/Funcion--------------*/  
      If @VSCO_BALA = '11'  
        Begin  
         Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT,  
           DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER,ACTIVO,   
           PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
         Values (RTRIM(@CSCO_CNTA),RTRIM(@CSNO_CNTA),@CNIM_SALD_ANTE,@VNIM_DEBE_MESE,  
          ( @VNIM_HABE_MESE ), 0, Abs(@VNTO_CALC),0, 0, 0, Abs(@VNTO_CALC),  
           0, Abs(@VNTO_CALC) )  
        End  
       Else     /*------- GANANCIAS y PERDIDAS por Funci=n ---------*/  
      If @VSCO_BALA = '01'  
       Begin  
         Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP,SALD_ANT,       
            DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER, ACTIVO,   
            PASIVO, PERDIDAS,GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
         Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_SALD_ANTE, @VNIM_DEBE_MESE,  
            @VNIM_HABE_MESE, 0, Abs(@VNTO_CALC), 0, 0, 0, 0, 0, Abs(@VNTO_CALC))  
      End  
      Else  
       Begin  
         Insert #TWSALD_CORP_Q07_1(CO_CNTA_CORP,DE_CNTA_CORP,SALD_ANT,  
           DEBE_MES,HABER_MES, SALD_DEBE, SALD_HABER,ACTIVO,  
           PASIVO,PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN )   
         Values (RTRIM(@CSCO_CNTA),RTRIM(@CSNO_CNTA),@CNIM_SALD_ANTE,   
           @VNIM_DEBE_MESE, (@VNIM_HABE_MESE), 0, Abs(@VNTO_CALC), 0, 0, 0, 0, 0 ,0 )  
       End   
     End  
   
  Fetch CU_TASALD_CORP_Q07_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_SALD_ANTE, @CSCO_BALA  
End  
                                                                                                                                                                                                                            
Close CU_TASALD_CORP_Q07_1  
Deallocate CU_TASALD_CORP_Q07_1  
  
--- TODAS LAS FILAS   
If @ISFL_MONT = 'T'  
Begin   
  
If @ISTI_REPO = 'REP'  
Begin  
If @ISTI_PRES = 'U'   
   Select CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT, DEBE_MES,  
          HABER_MES, SALD_DEBE, SALD_HABER,  
          ACTIVO, PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN,  
          NULL,    NULL,  
          NULL,    NULL,    NULL,  
          NULL,    NULL,    NULL,   
          NULL,    NULL,    NULL,  'A', 1, 0
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP,  T2.DE_CNTA_CORP,    
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
   Isnull(SUM(Isnull(T1.SALD_ANT, 0)), 0),   
   Isnull(SUM(Isnull(T1.DEBE_MES, 0)), 0),     Isnull(SUM(Isnull(T1.HABER_MES, 0)), 0),     Isnull(SUM(Isnull(T1.SALD_DEBE, 0)), 0),      
   Isnull(SUM(Isnull(T1.SALD_HABER, 0)), 0), Isnull(SUM(Isnull(T1.ACTIVO, 0)), 0),  Isnull(SUM(Isnull(T1.PASIVO, 0)), 0),        
   Isnull(SUM(Isnull(T1.PERDIDAS, 0)), 0),   
   Isnull(SUM(Isnull(T1.GANANCIAS, 0)), 0),    Isnull(SUM(Isnull(T1.PERDIDAFUN, 0)), 0),     Isnull(SUM(Isnull(T1.GANANCIAFUN, 0)), 0), 'B',  
   0,  1   
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'E'    

   Select CO_CNTA_CORP,DE_CNTA_CORP,  Round(SALD_ANT/1000,2),  Round(DEBE_MES/1000,2),  
   Round(HABER_MES/1000,2),  Round(SALD_DEBE/1000,2), Round(SALD_HABER/1000,2),  
   Round(ACTIVO/1000,2),  Round(PASIVO/1000,2),   Round(PERDIDAS/1000,2),   
   Round(GANANCIAS/1000,2),  Round(PERDIDAFUN/1000,2),  Round(GANANCIAFUN/1000,2),  
   NULL,    NULL,  
   NULL,    NULL,    NULL,  
   NULL,    NULL,     NULL,   
   NULL,    NULL,     NULL,  
   'A', 1, 0  
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP,   T2.DE_CNTA_CORP,      
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000,2)), 0),   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000,2)), 0),    'B', 0, 1  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'O'    

   Select CO_CNTA_CORP,DE_CNTA_CORP, Round(SALD_ANT/1000000,2), Round(DEBE_MES/1000000,2),           Round(HABER_MES/1000000,2), Round(SALD_DEBE/1000000,2), Round(SALD_HABER/1000000,2),                                                                           
   Round(ACTIVO/1000000,2), Round(PASIVO/1000000,2), Round(PERDIDAS/1000000,2),            Round(GANANCIAS/1000000,2), Round(PERDIDAFUN/1000000,2), Round(GANANCIAFUN/1000000,2) ,  
   NULL,    NULL,  
   NULL,    NULL,    NULL,  
   NULL,    NULL,     NULL,   
   NULL,    NULL,     NULL,  
   'A', 1, 0   
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP, T2.DE_CNTA_CORP,    
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000000,2)), 0),   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000000,2)), 0),           Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000000,2)), 0),  Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000000,2)), 0),   Isnull
   (  
   SUM(Round(Isnull(T1.SALD_HABER, 0)/1000000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000000,2)), 0),     
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000000,2)), 0),              
   Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000000,2)), 0)  ,  
   'B', 0, 1  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE   
   DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1 End  
Else  
If @ISTI_REPO = 'TAB'  
Begin  
If @ISTI_PRES = 'U'   
   Select CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT, DEBE_MES,  
          HABER_MES, SALD_DEBE, SALD_HABER,  
          ACTIVO, PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN  
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP,  T2.DE_CNTA_CORP,    
   Isnull(SUM(Isnull(T1.SALD_ANT, 0)), 0),   
   Isnull(SUM(Isnull(T1.DEBE_MES, 0)), 0),     Isnull(SUM(Isnull(T1.HABER_MES, 0)), 0),     Isnull(SUM(Isnull(T1.SALD_DEBE, 0)), 0),      
   Isnull(SUM(Isnull(T1.SALD_HABER, 0)), 0), Isnull(SUM(Isnull(T1.ACTIVO, 0)), 0),  Isnull(SUM(Isnull(T1.PASIVO, 0)), 0),        
   Isnull(SUM(Isnull(T1.PERDIDAS, 0)), 0),   
   Isnull(SUM(Isnull(T1.GANANCIAS, 0)), 0),    Isnull(SUM(Isnull(T1.PERDIDAFUN, 0)), 0),     Isnull(SUM(Isnull(T1.GANANCIAFUN, 0)), 0)  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'E'    

   Select CO_CNTA_CORP,DE_CNTA_CORP,  Round(SALD_ANT/1000,2),  Round(DEBE_MES/1000,2),  
   Round(HABER_MES/1000,2),  Round(SALD_DEBE/1000,2), Round(SALD_HABER/1000,2),  
   Round(ACTIVO/1000,2),  Round(PASIVO/1000,2),   Round(PERDIDAS/1000,2),   
   Round(GANANCIAS/1000,2),  Round(PERDIDAFUN/1000,2),  Round(GANANCIAFUN/1000,2)  
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP,   T2.DE_CNTA_CORP,      
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000,2)), 0),   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000,2)), 0)  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'O'    
   Select CO_CNTA_CORP,DE_CNTA_CORP, Round(SALD_ANT/1000000,2), Round(DEBE_MES/1000000,2),  Round(HABER_MES/1000000,2), Round(SALD_DEBE/1000000,2), Round(SALD_HABER/1000000,2),                                                                                           
   Round(ACTIVO/1000000,2), Round(PASIVO/1000000,2), Round(PERDIDAS/1000000,2),            Round(GANANCIAS/1000000,2), Round(PERDIDAFUN/1000000,2), Round(GANANCIAFUN/1000000,2)  
   From #TWSALD_CORP_Q07_1  
   UNION ALL  
   Select T2.CO_CNTA_CORP, T2.DE_CNTA_CORP,    
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000000,2)), 0),             
          Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000000,2)), 0),   
          Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000000,2)), 0),              
          Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000000,2)), 0)    
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE   
   DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP LIKE  T2.CO_CNTA_CORP + '%'  
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1 End                              
End   
  
--------  
--------  
--- SOLO FILAS  CON MONTOS   
If @ISFL_MONT = 'M'  
Begin   
  
If @ISTI_REPO = 'REP'  
Begin  
If @ISTI_PRES = 'U'   
   Select CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT, DEBE_MES,  
          HABER_MES, SALD_DEBE, SALD_HABER,  
          ACTIVO, PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN,  
          NULL,    NULL,  
          NULL,    NULL,    NULL,  
          NULL,    NULL,     NULL,   
          NULL,    NULL,     NULL,  'A', 1, 0  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP,  T2.DE_CNTA_CORP,    
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
   Isnull(SUM(Isnull(T1.SALD_ANT, 0)), 0),   
   Isnull(SUM(Isnull(T1.DEBE_MES, 0)), 0),     Isnull(SUM(Isnull(T1.HABER_MES, 0)), 0),     Isnull(SUM(Isnull(T1.SALD_DEBE, 0)), 0),      
   Isnull(SUM(Isnull(T1.SALD_HABER, 0)), 0), Isnull(SUM(Isnull(T1.ACTIVO, 0)), 0),  Isnull(SUM(Isnull(T1.PASIVO, 0)), 0),        
   Isnull(SUM(Isnull(T1.PERDIDAS, 0)), 0),   
   Isnull(SUM(Isnull(T1.GANANCIAS, 0)), 0),    Isnull(SUM(Isnull(T1.PERDIDAFUN, 0)), 0),     Isnull(SUM(Isnull(T1.GANANCIAFUN, 0)), 0), 'B', 
   0, 1  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'E'    

   Select CO_CNTA_CORP,DE_CNTA_CORP,  Round(SALD_ANT/1000,2),  Round(DEBE_MES/1000,2),  
   Round(HABER_MES/1000,2),  Round(SALD_DEBE/1000,2), Round(SALD_HABER/1000,2),  
   Round(ACTIVO/1000,2),  Round(PASIVO/1000,2),   Round(PERDIDAS/1000,2),   
   Round(GANANCIAS/1000,2),  Round(PERDIDAFUN/1000,2),  Round(GANANCIAFUN/1000,2),  
   NULL,    NULL,  
   NULL,    NULL,    NULL,  
   NULL,    NULL,     NULL,   
   NULL,    NULL,     NULL,  
   'A', 1, 0  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP,   T2.DE_CNTA_CORP,      
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
          Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000,2)), 0),   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000,2)), 0),    'B', 0, 1  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'O'    
   Select CO_CNTA_CORP,DE_CNTA_CORP, Round(SALD_ANT/1000000,2), Round(DEBE_MES/1000000,2),           Round(HABER_MES/1000000,2), Round(SALD_DEBE/1000000,2), Round(SALD_HABER/1000000,2),                                                                                                 
   Round(ACTIVO/1000000,2), Round(PASIVO/1000000,2), Round(PERDIDAS/1000000,2),            Round(GANANCIAS/1000000,2), Round(PERDIDAFUN/1000000,2), Round(GANANCIAFUN/1000000,2) ,  
   NULL,    NULL,  
   NULL,    NULL,    NULL,  
   NULL,    NULL,     NULL,   
   NULL,    NULL,     NULL,  
   'A', 1, 0  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP, T2.DE_CNTA_CORP,    
   NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,     NULL,      NULL,   
   NULL,  
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000000,2)), 0),           
   Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000000,2)), 0),  
   Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000000,2)), 0),     
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000000,2)), 0),              
   Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000000,2)), 0),    
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000000,2)), 0)  ,  
   'B', 0, 1  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE   
   DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1 
 End  
Else  
If @ISTI_REPO = 'TAB'  
Begin  
If @ISTI_PRES = 'U'   
   Select CO_CNTA_CORP,DE_CNTA_CORP, SALD_ANT, DEBE_MES,  
          HABER_MES, SALD_DEBE, SALD_HABER,  
          ACTIVO, PASIVO, PERDIDAS, GANANCIAS, PERDIDAFUN, GANANCIAFUN  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP,  T2.DE_CNTA_CORP,    
   Isnull(SUM(Isnull(T1.SALD_ANT, 0)), 0),   
   Isnull(SUM(Isnull(T1.DEBE_MES, 0)), 0),     Isnull(SUM(Isnull(T1.HABER_MES, 0)), 0),     Isnull(SUM(Isnull(T1.SALD_DEBE, 0)), 0),      
   Isnull(SUM(Isnull(T1.SALD_HABER, 0)), 0), Isnull(SUM(Isnull(T1.ACTIVO, 0)), 0),  Isnull(SUM(Isnull(T1.PASIVO, 0)), 0),        
   Isnull(SUM(Isnull(T1.PERDIDAS, 0)), 0),   
   Isnull(SUM(Isnull(T1.GANANCIAS, 0)), 0),    Isnull(SUM(Isnull(T1.PERDIDAFUN, 0)), 0),     Isnull(SUM(Isnull(T1.GANANCIAFUN, 0)), 0)  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'E'    
   Select CO_CNTA_CORP,DE_CNTA_CORP,  Round(SALD_ANT/1000,2),  Round(DEBE_MES/1000,2),  
          Round(HABER_MES/1000,2),  Round(SALD_DEBE/1000,2), Round(SALD_HABER/1000,2),  
          Round(ACTIVO/1000,2),  Round(PASIVO/1000,2),   Round(PERDIDAS/1000,2),   
          Round(GANANCIAS/1000,2),  Round(PERDIDAFUN/1000,2),  Round(GANANCIAFUN/1000,2)  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP,   T2.DE_CNTA_CORP,      
          Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000,2)), 0), Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000,2)), 0),   Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000,2)), 0),  Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000,2)), 0),   
   Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000,2)), 0)  
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP  LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1  
Else If @ISTI_PRES = 'O'    
   Select CO_CNTA_CORP,DE_CNTA_CORP, Round(SALD_ANT/1000000,2), Round(DEBE_MES/1000000,2),           Round(HABER_MES/1000000,2), Round(SALD_DEBE/1000000,2), Round(SALD_HABER/1000000,2),                                                                                                    
   Round(ACTIVO/1000000,2), Round(PASIVO/1000000,2), Round(PERDIDAS/1000000,2),            Round(GANANCIAS/1000000,2), Round(PERDIDAFUN/1000000,2), Round(GANANCIAFUN/1000000,2)  
   From #TWSALD_CORP_Q07_1  
   Where Abs(SALD_ANT) > 0 Or Abs(DEBE_MES) > 0 Or Abs(HABER_MES) > 0 Or     
   Abs(SALD_DEBE) > 0 Or Abs(SALD_HABER) > 0   
   UNION ALL  
   Select T2.CO_CNTA_CORP, T2.DE_CNTA_CORP,    
   Isnull(SUM(Round(Isnull(T1.SALD_ANT, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.DEBE_MES, 0)/1000000,2)), 0),             
          Isnull(SUM(Round(Isnull(T1.HABER_MES, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.SALD_DEBE, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.SALD_HABER, 0)/1000000,2)), 0),   
          Isnull(SUM(Round(Isnull(T1.ACTIVO, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.PASIVO, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.PERDIDAS, 0)/1000000,2)), 0),              
          Isnull(SUM(Round(Isnull(T1.GANANCIAS, 0)/1000000,2)), 0),    
          Isnull(SUM(Round(Isnull(T1.PERDIDAFUN, 0)/1000000,2)), 0),     
          Isnull(SUM(Round(Isnull(T1.GANANCIAFUN, 0)/1000000,2)), 0)    
   From #TWSALD_CORP_Q07_1 T1, TMPLAN_CORP T2  
   WHERE   
   DATALENGTH(T2.CO_CNTA_CORP) < @INNU_DGTO  
   AND T1.CO_CNTA_CORP LIKE  T2.CO_CNTA_CORP + '%'  
   And (Abs(t1.SALD_ANT) > 0 Or Abs(t1.DEBE_MES) > 0 Or Abs(t1.HABER_MES) > 0 Or     
   Abs(t1.SALD_DEBE) > 0 Or Abs(t1.SALD_HABER) > 0)   
   GROUP BY T2.CO_CNTA_CORP, T2.DE_CNTA_CORP  
   Order by 1 End                              
End   
return                                                                                                                                                   
/*----------------------------------- Fin ------------------------------------------*/  


GO
